package de.tum.in.eist.poll.server;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.tum.in.eist.poll.server.persistence.AppEngineDatabase;
import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.User;
import de.tum.in.eist.poll.shared.service.PollService;

@SuppressWarnings("serial")
public class PollServiceImpl extends RemoteServiceServlet implements
		PollService {

	private static AppEngineDatabase db = AppEngineDatabase.getInstance();
	
	@Override
	public User login(String email, String psswd)
			throws IllegalArgumentException {
		User user = db.validateUser(email, psswd);
		if (user != null) {
//			System.out.println(user);
			db.clearAllNotifications(user);
			user.setNotificationKeys(null);
			return user;
		} else {
			throw new IllegalArgumentException("wrong username or password");
		}
	}

	@Override
	public Lecture addNewLecture(User user, String topic, Date date)
			throws IllegalArgumentException {
		if (!user.isLecturer())
			throw new IllegalArgumentException(
					"only lecturers may create new lectures");
		else
			return db.storeLecture(new Lecture(topic, user, date));
	}

	@Override
	public Lecture postFeedback(User user, Lecture lecture, Feedback feedback)
			throws Exception {
		Lecture result = db.addFeedbackToLecture(feedback, lecture);
		NotificationsPushServiceImpl.addNotification(result);
		return result;
	}
	
	@Override
	public User login(String sessionID) {
		User user = db.validateSession(sessionID);
		db.clearAllNotifications(user);
		user.setNotificationKeys(null);
//		System.out.println(user);
		return user;
	}

	@Override
	public ArrayList<Lecture> requestLecturesInRange(User user, Date start,
			Date stop) {
		return db.getLecturesFromTo(start, stop);
	}

	@Override
	public ArrayList<Lecture> requestLecturesOfCurrentWeek(User user) {
		ArrayList<Lecture> result =  db.getLecturesFromTo(CalendarTools.getStartOfCurrentWeek(),
				CalendarTools.getEndOfCurrentWeek());
		return result;
	}

	@Override
	public ArrayList<Lecture> requestLecturesOfLastWeek(User user) {
		return db.getLecturesFromTo(CalendarTools.getStartOfPreviousWeek(),
				CalendarTools.getStartOfCurrentWeek());
	}

	/**
	 * not implemented yet (returns empty list)
	 */
	@Override
	public ArrayList<Lecture> requestUpdatedLectures(User user) {
		return new ArrayList<Lecture>();
	}

	@Override
	public ArrayList<Lecture> requestLecturesOfCurrentSemester(User user) {
		if (CalendarTools.getWS().before(Calendar.getInstance().getTime())) {
			Date start = CalendarTools.getWS();
			Date end = CalendarTools.getSS();
			return db.getLecturesFromTo(start, end);
		} else {
			Date end = CalendarTools.getWS();
			Date start = CalendarTools.getSS();
			return db.getLecturesFromTo(start, end);
		}
	}

	@Override
	public User register(String email, String pass)
			throws IllegalArgumentException {
		if (db.validateUser(email, pass) != null)
			throw new IllegalArgumentException(
					"user with this email adress already exists");
		db.storeUser(new User(email, pass));
		return db.validateUser(email, pass);
	}

	@Override
	public ArrayList<Lecture> addMultipleNewLectures(User user, String topic,
			Date startdate, Date enddate) throws IllegalArgumentException {
		ArrayList<Lecture> result = new ArrayList<Lecture>();
		Calendar cal = Calendar.getInstance();
		cal.setTime(startdate);
		while(cal.getTime().before(enddate) || cal.getTime().equals(enddate)) {
			result.add(addNewLecture(user, topic, cal.getTime()));
			cal.add(Calendar.DATE, 7);
		}
		return result;
	}

}
